/**
 *
 */
package de.cbf.cam.dsa.facet.garment;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import de.cbf.cam.dsa.facet.garment.interfaces.IGenderSpecific;
import de.cbf.util.entity.facet.constants.ConstantsFontColorLogView;
import de.cbf.util.entity.facet.entities.FacetOfEntity;
import de.cbf.util.entity.facet.util.TextPartStyled;

/**
 * @author Nebelritter
 *
 */
@XmlRootElement(name="gament")
@XmlAccessorType(XmlAccessType.FIELD)
public class FacetGarment extends FacetOfEntity implements Comparable<FacetGarment>, IGenderSpecific,FacetGarmentConstants{
   
		
	protected Double fLayer;
    /**
     * if collision occurs shift fLayer by this amount
     */
    protected Double fLayerShiftIndex;
    @XmlElementWrapper(name="wearplaces")
    @XmlElement(name="place")
    protected List<String> fCoveredWearPlaces;

    /** integer for suiting gender
     */
    protected int fSuitableGender;

    protected String fColor;
    protected String fMaterial;

    protected Double fColdResistance;
    
    public FacetGarment() {
        super();
    }

    public FacetGarment(String name) {
        super(name);
    }

    @Override
    protected void init() {
        super.init();
        fLayer 				= UNSET_LAYER;
        fLayerShiftIndex	= UNSET_LAYER_SHIFT_INDEX;
        fCoveredWearPlaces	= UNSET_COVERED_WEAR_PLACES;        
        fSuitableGender		= UNSET_SUITABLE_GENDER;
        fColor				= UNSET_COLOR;
        fMaterial			= UNSET_MATERIAL;
        fColdResistance		= UNSET_COLD_RESISTANCE;
    }

    @Override
    public int compareTo(FacetGarment other) {
        if(fLayer.equals(other.fLayer)){
            return 0;
        }
        if(fLayer > other.fLayer){
            return 1;
        }
        return -1;
    }
    
    
    @Override
	public List<TextPartStyled> getFacetStyledDescription() {
		
    	List<TextPartStyled> textStyleInfo = new ArrayList<TextPartStyled>();
		
		TextPartStyled textPart;
			
		//garment heading
		textPart = new TextPartStyled();
		textPart.setText(TEXT_LOG_HEADING_GARMENT+"\n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_FACET);		
		textStyleInfo.add(textPart );
				
		//fLayer
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_LAYER);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart );
		//fLayer value
		textPart = new TextPartStyled();
		textPart.setText(fLayer+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_VALUE);		
		textStyleInfo.add(textPart );

		//fLayer shift index
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_LAYER_SHIFT_INDEX);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart);
		//fLayer shift index value
		textPart = new TextPartStyled();
		textPart.setText(fLayerShiftIndex+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_VALUE);		
		textStyleInfo.add(textPart);
		
		//wear places
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_WEAR_PLACES);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart);
		//wear places value
		textPart = new TextPartStyled();
		textPart.setText(fCoveredWearPlaces+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_VALUE);		
		textStyleInfo.add(textPart);

		//gender
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_GENDER);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart);
		//gender value
		String genderText = IGenderSpecific.Util.getGenderText(fSuitableGender);
		textPart = new TextPartStyled();
		textPart.setText(genderText+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_VALUE);		
		textStyleInfo.add(textPart);
		
		//fColor
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_COLOR);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart);
		//fColor value	
		textPart = new TextPartStyled();
		textPart.setText(fColor+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_VALUE);		
		textStyleInfo.add(textPart);

		//fMaterial
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_MATERIAL);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart);
		//fMaterial value	
		textPart = new TextPartStyled();
		textPart.setText(fMaterial+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_VALUE);		
		textStyleInfo.add(textPart);
		
		//fMaterial
		textPart = new TextPartStyled();
		textPart.setText("\t"+TEXT_LOG_COLD_RESISTANCE);
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_CATEGORY);		
		textStyleInfo.add(textPart);
		//fMaterial value	
		textPart = new TextPartStyled();
		textPart.setText(fColdResistance+" \n");
		textPart.setStyleConstant(ConstantsFontColorLogView.STYLE_VALUE);		
		textStyleInfo.add(textPart);
		
		return textStyleInfo;
	}
    
    @Override
	public String hasUnsetValues() {
		StringBuffer result = new StringBuffer();
		if(UNSET_LAYER.equals(fLayer)){
			result.append(TEXT_UNSET_LAYER+"\n");
		}
		if(UNSET_LAYER_SHIFT_INDEX.equals(fLayerShiftIndex)){
			result.append(TEXT_UNSET_LAYER_SHIFT_INDEX+"\n");
		}
		if(fCoveredWearPlaces.isEmpty()){
			result.append(TEXT_UNSET_COVERED_WEAR_PLACES+"\n");
		}
		if(UNSET_SUITABLE_GENDER == fSuitableGender){
			result.append(TEXT_UNSET_SUITABLE_GENDER+"\n");
		}
		if(UNSET_COLOR.equals(fColor)){
			result.append(TEXT_UNSET_COLOR+"\n");
		}
		if(UNSET_MATERIAL.equals(fMaterial)){
			result.append(TEXT_UNSET_MATERIAL+"\n");
		}
		if(UNSET_COLD_RESISTANCE.equals(fColdResistance)){
			result.append(TEXT_UNSET_COLD_RESISTANCE+"\n");
		}
		return result.toString();
	}


    public Double getLayer() {
        return fLayer;
    }

    public void setLayer(Double newLayer) {
        if(fLayer != null && fLayer.equals(newLayer)){
            return;
        }
        Double oldLayer = fLayer;
        fLayer = newLayer;
        propertyChangeSupport.fireIndexedPropertyChange(PN_LAYER, PI_LAYER, oldLayer, fLayer);
    }

    public List<String> getCoveredWearPlaces() {
        return fCoveredWearPlaces;
    }

    public void addWearPlace(String wearPlace){
        boolean worked = fCoveredWearPlaces.add(wearPlace);
        if(worked){
        	propertyChangeSupport.fireIndexedPropertyChange(PN_COVERED_WEAR_PLACES, PI_COVERED_WEAR_PLACES, null,fCoveredWearPlaces);
        }
    }

    public void removeWearPlace(String wearPlace){
        boolean worked = fCoveredWearPlaces.remove(wearPlace);
        if(worked){
        	propertyChangeSupport.fireIndexedPropertyChange(PN_COVERED_WEAR_PLACES, PI_COVERED_WEAR_PLACES, null,fCoveredWearPlaces);
        }
    }

    public void setCoveredWearPlaces(List<String> newCoveredWearPlaces) {
    	if(fCoveredWearPlaces != null && fCoveredWearPlaces.equals(newCoveredWearPlaces)){
    		return;
    	}
        fCoveredWearPlaces = newCoveredWearPlaces;
        propertyChangeSupport.fireIndexedPropertyChange(PN_COVERED_WEAR_PLACES, PI_COVERED_WEAR_PLACES, null,fCoveredWearPlaces);
    }

    public int getGender() {
        return fSuitableGender;
    }

    public void setGender(int newGender) {
        if(fSuitableGender == newGender){
            return;
        }
        int oldGender = fSuitableGender;
        fSuitableGender = newGender;
        propertyChangeSupport.fireIndexedPropertyChange(PN_SUITABLE_GENDER, PI_SUITABLE_GENDER, oldGender, fSuitableGender);
    }

    public Double getLayerShiftIndex() {
        return fLayerShiftIndex;
    }

    public void setLayerShiftIndex(Double newLayerShiftIndex) {
        if(fLayerShiftIndex != null && fLayerShiftIndex.equals(newLayerShiftIndex)){
            return;
        }
        Double oldLayerShiftIndex = fLayerShiftIndex;
        fLayerShiftIndex = newLayerShiftIndex;
        propertyChangeSupport.fireIndexedPropertyChange(PN_LAYER_SHIFT_INDEX, PI_LAYER_SHIFT_INDEX, oldLayerShiftIndex, fLayerShiftIndex);
    }

	public String getColor() {
		return fColor;
	}

	public void setColor(String newColor) {
		if(fColor != null && fColor.equals(newColor)){
            return;
        }
        String oldColor = fColor;
        fColor = newColor;
        propertyChangeSupport.fireIndexedPropertyChange(PN_COLOR, PI_COLOR, oldColor, fColor);
	}

	public String getMaterial() {
		return fMaterial;
	}

	public void setMaterial(String newMaterial) {
		if(fMaterial != null && fMaterial.equals(newMaterial)){
            return;
        }
        String oldmaterial = fMaterial;
        fMaterial = newMaterial;
        propertyChangeSupport.fireIndexedPropertyChange(PN_MATERIAL, PI_MATERIAL, oldmaterial, fMaterial);
	}

	/**
	 * @return the fColdResistance
	 */
	public Double getColdResistance() {
		return fColdResistance;
	}

	/**
	 * @param fColdResistance the fColdResistance to set
	 */
	public void setColdResistance(Double newColdResistance) {
		if(fColdResistance != null && fColdResistance.equals(newColdResistance)){
            return;
        }
        Double oldColdResistance = fColdResistance;
        fColdResistance = newColdResistance;
        propertyChangeSupport.fireIndexedPropertyChange(PN_COLD_RESISTANCE, PI_COLD_RESISTANCE, oldColdResistance, fColdResistance);
	}

}
